package com.api.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.api.config.CachService;
import com.api.config.RedisConstant;
import com.api.config.RedisService;
import com.api.domain.UserDTO;
import com.api.entity.Menus;
import com.api.entity.User;
import com.api.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.Map;

import static com.api.entity.table.MenusTableDef.MENUS;
import static com.api.entity.table.RoleRightTableDef.ROLE_RIGHT;

/**
 * @Description: 描述
 * @Author: yangshfieng
 * @CreateDate: 2023/6/25
 */
@Controller
public class BaseController {


    @Autowired
    CachService cachService;
    @Autowired
    RedisService redisService;


    /**
     * 查询 登录用户ID 信息
     * 接口：/getUserId
     * @return
     */
    public Long getUserId(){
        Long loginId = StpUtil.getLoginIdAsLong();
        return loginId;
    }

    public Integer getRoleType(){
        Integer roleType = null;
        String loginId = StpUtil.getLoginIdAsString();
        if(null == loginId){
            throw new NotLoginException("未获取到登录信息","","-1");
        }
        if(redisService.hasKey(RedisConstant.USER_INFO + loginId)){
            Object cacheObject = redisService.getCacheObject(RedisConstant.USER_INFO + loginId);
            Map<String, Object> map = BeanUtil.beanToMap(cacheObject);
            if(MapUtil.isNotEmpty(map) && null != map.get("roleType")){
                roleType = Integer.parseInt(map.get("roleType").toString());
            }
        }
        else{
            roleType = getUserInfo().getRoleType();
        }
        return roleType;
    }


    public Long getRoleId(){
        Long roleId = null;
        String loginId = StpUtil.getLoginIdAsString();
        if(null == loginId){
            throw new NotLoginException("未获取到登录信息","","-1");
        }
        if(redisService.hasKey(RedisConstant.USER_INFO + loginId)){
            Object cacheObject = redisService.getCacheObject(RedisConstant.USER_INFO + loginId);
            Map<String, Object> map = BeanUtil.beanToMap(cacheObject);
            if(MapUtil.isNotEmpty(map) && null != map.get("roleId")){
                roleId = Long.parseLong(map.get("roleId").toString());
            }
        }
        else{
            roleId = getUserInfo().getRoleId();
        }
        return roleId;
    }

    /**
     * 查询 用户 信息
     * @return
     */
    public UserDTO getUserInfo(){
        Object loginId = StpUtil.getLoginIdDefaultNull();
        if(null == loginId){
            throw new NotLoginException("未获取到登录信息","","-1");
        }
        Long userId = Long.parseLong(loginId.toString());
        System.out.println("当前登录用户的UserId  ==>  "+userId);
        User user = User.create()
                .setId(userId)
                .oneById();
        return cachService.setCachUser(user);
    }

    /**
     * 查询 Token 信息
     * 接口：/tokenInfo
     * @return
     */
    public Result tokenInfo() {
        return Result.ok(StpUtil.getTokenInfo());
    }

    public Long getDeptId(){
        Long deptId = null;
        String loginId = StpUtil.getLoginIdAsString();
        if(null == loginId){
            throw new NotLoginException("未获取到登录信息","","-1");
        }
        if(redisService.hasKey(RedisConstant.USER_INFO + loginId)){
            Object cacheObject = redisService.getCacheObject(RedisConstant.USER_INFO + loginId);
            Map<String, Object> map = BeanUtil.beanToMap(cacheObject);
            if(MapUtil.isNotEmpty(map) && null != map.get("deptId")){
                deptId = Long.parseLong(map.get("deptId").toString());
            }
        }
        else{
            deptId = getUserInfo().getRoleId();
        }
        return deptId;
    }

    /**
     * 根据菜单，查询当前登录用户的菜单权限
     *
     * @return
     */
    public int getMenuRight(String menuUrl){
//        int kind = 0;
//        Long roleId = getRoleId();
//        Menus menu = Menus.create()
//            .select(MENUS.ID, MENUS.TITLE, MENUS.MENU_TYPE, ROLE_RIGHT.KIND)
//            .leftJoin(ROLE_RIGHT).on(ROLE_RIGHT.MENU_ID.eq(MENUS.ID))
//            .where(MENUS.MENU_URL.eq(menuUrl)).and(MENUS.STATUS.eq(1)).and(MENUS.KIND.eq(1))
//            .and(ROLE_RIGHT.ROLE_ID.eq(roleId)).and(ROLE_RIGHT.STATUS.eq(1))
//            .one();
//        if(null != menu && null != menu.getKind()){
//            kind = menu.getKind();
//        }
//        return kind;

        return 1;
    }

}
